package com.jnshu.controller;

import com.jnshu.constant.Constant;
import com.jnshu.dto.admin.ManageDto;
import com.jnshu.dto.admin.PasswordDto;
import com.jnshu.service.ManageService;
import com.jnshu.tools.HeadUtil;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
public class ManageCon {

    @Resource
    ManageService manageService;

    @GetMapping("/manage/list")
    public PageBean manageList(@RequestParam(name = "pageSize",defaultValue = "10")int pageSize,
                               @RequestParam(name = "pageNum",defaultValue = "1") int pageNum){
        log.info ("_____________________________________________");
        log.info("方法manageList接收到的请求参数为："+pageNum+","+pageSize);
        return manageService.selectManageList(pageNum,pageSize);
    }

    @DeleteMapping(value = "/manage/{id}")
    public ResultBean deleteManage(@PathVariable("id")long id){
        log.info ("_____________________________________________");
        log.info("方法deleteManage接收到的请求参数为："+id);
        return manageService.deleteByPrimaryKey(id);
    }

    @PutMapping(value = "/manage/",produces = {"application/json;charset=UTF-8"})
    public ResultBean updateManage(@RequestBody ManageDto manageDto){
        log.info ("_____________________________________________");
        log.info("方法updateManage接收到的请求参数为："+ JsonUtil.toJsonString(manageDto));
        return manageService.updateByPrimaryKeySelective(manageDto,HeadUtil.getHeadValue(Constant.LOGINID));
    }

    @PostMapping(value = "/manage",produces = {"application/json;charset=UTF-8"})
    public ResultBean insertManage(@RequestBody ManageDto manageDto){
        log.info ("_____________________________________________");
        log.info("方法insertManage接收到的请求参数为："+JsonUtil.toJsonString(manageDto));
        return manageService.insertSelective(manageDto,"1");
    }

    @GetMapping(value = "/manage/role/{id}")
    public PageBean selectByRole(@RequestParam(name = "pageSize",defaultValue = "10")int pageSize,
                                 @RequestParam(name = "pageNum",defaultValue = "1")int pageNum,
                                 @PathVariable("id")long id){
        log.info ("_____________________________________________");
        log.info("方法selectByRole接收到的请求参数为："+id+","+pageNum+","+pageSize);
        return manageService.selectByRoleId(id,pageNum,pageSize);
    }

    @PutMapping (value = "/manage/password")
    public ResultBean updateLoginPassword(@RequestBody PasswordDto passwordDto){
        log.info ("_____________________________________________");
        log.info("方法updateLoginPassword接收到的请求参数为："+JsonUtil.toJsonString(passwordDto));
        return manageService.updateByLoginPassword(passwordDto, HeadUtil.getHeadValue(Constant.LOGINID));
    }

    @GetMapping(value = "/manage/{id}",produces = {"application/json;charset=UTF-8"})
    public ResultBean selectByManageId(@PathVariable("id")long id){
        log.info ("_____________________________________________");
        log.info("方法selectByManageId接收到的请求参数为："+id);
        return manageService.selectByManageId(id);
    }
}
